<?php



class Mysql_lightDriver extends DBDriver
{
	private $resultvar;
	
	public function __construct()
	{
		$this->query = "";
	}
	
	public function connect( $host, $user, $password, $database )
	{
		$this->resource = mysql_connect( $host, $user, $password, true );
		if ( $this->resource == false )
		{
			trigger_error("Error connection to database");
		}
		
		mysql_select_db( $database, $this->resource );
	}
	
	
	public function encoding( $enc )
	{
		mysql_query		( "SET NAMES '".$enc."'" );
		mysql_query		( "SET character_set_results = '".$enc."'" );
		mysql_query		( "SET character_set_client = '".$enc."'" );
		mysql_query		( "SET CHARACTER SET '".$enc."'" );
	}
	
	
	public function __call( $method, $args )
	{
		parent::_call( $method, $args, __CLASS__ );
	}
	
	
	public function select( $fields, $table )
	{
		$this->query = "SELECT $fields FROM `$table`";
		
		return $this;
	}
	
	public function update( $table )
	{
		$this->query = "UPDATE `$table`";
		
		return $this;
	}
	
	public function query( $query )
	{
		$this->query = $query;
		
		return $this;
	}
	
	
	public function truncate( $table )
	{
		$this->query = "TRUNCATE TABLE `$table`";
		
		return $this;
	}
	
	
	public function insert( $table )
	{
		$this->query = "INSERT INTO `$table`";
		
		return $this;
	}
	
	
	public function remove( $table )
	{
		$this->query = "DELETE FROM `$table`";
		
		return $this;
	}
	
	
	public function where( $usl )
	{
		$this->query .= " WHERE $usl";
		
		return $this;
	}
	
	
	public function order( $field, $asc = "ASC" )
	{
		$this->query .= " ORDER BY `$field` $asc";
		
		return $this;
	}
	
	
	public function limit( $start = false, $count = false )
	{
		if ( !$start )
		{
			$this->query .= " LIMIT 500";
			return $this;
		}
		
		if ( $count == false)
		{
			$this->query .= " LIMIT $start";
		}
		else
		{
			$this->query .= " LIMIT $start, $count";
		}
		
		return $this;
	}
	
	
	public function values( $fields, $values )
	{
		if ( is_array($fields) && is_array($values) )
		{
			if ( count( $fields ) == count($values) )
			{
				$f = "";
				$v = "";
				$cnt = count($fields);
				
				for( $i = 0; $i < $cnt; $i++ )
				{
					if ( $i > 0 )
					{
						$f .= ", ";
						$v .= ", ";
					}
					
					$f .= "`" . $fields[$i] . "`";
					$v .= "" . ( $values[$i] == null ) ?
								"NULL" : "'" . $values[$i] . "'";
				}
				
				$this->query .= '(' . $f . ') VALUES (' . $v . ')';
			}
			else
			{
				trigger_error( "Fields and Values arrays must have same length!" );
			}
		}
		else
		{
			$this->query .= '(' . $fields . ') VALUES (' . $values . ')';
		}
		
		return $this;
	}
	
	
	public function set( $fields, $values )
	{
		if ( is_array($fields) && is_array($values) )
		{
			if ( count( $fields ) == count($values) )
			{
				$q = "";
				$cnt = count($fields);
				
				for( $i = 0; $i < $cnt; $i++ )
				{
					if ( $i > 0 )
					{
						$q .= ",";
					}
					
					$q .= "`" . $fields[$i] . "`='".( $values[$i] == null ) ? "NULL" : "'" . $values[$i] . "'";
				}
				
				$this->query .= ' SET ' . $q;
			}
		}
		
		return $this;
	}
	
	
	public function q( $query )
	{
		$this->query .= $query;
		return $this;
	}
	
	
	public function result()
	{
		$this->resultvar = mysql_query($this->query, $this->resource);
		
		return $this;
	}
	
	
	public function fetch_bool()
	{
		return $this->resultvar ? true : false;
	}
	
	
	public function fetch_array()
	{
		$res = $this->resultvar;
		
		if ( $res != null && mysql_num_rows( $res ) > 0 )
		{
			return mysql_fetch_array( $res );
		}
		
		return false;
	}
	 
	
	public function fetch_assoc()
	{
		$res = $this->resultvar;
		
		if ( $res != null && mysql_num_rows( $res ) )
		{
			return mysql_fetch_assoc( $res );
		}
		
		return false;
	}
	
	
	public function while_fetch_array( &$h, $function )
	{
		$res = $this->resultvar;
		
		if ( $res != null && mysql_num_rows( $res ) )
		{
			while ( $arr = mysql_fetch_array( $res ) )
			{
				$function($arr, &$h);
			}
			return true;
		}
		
		return false;
	}
	
	public function has_rows()
	{
		$res = $this->resultvar;
		
		if ( $res != null && mysql_num_rows( $res ) > 0 )
			return true;
		
		return false;
	}
}



